<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="zjm">
          <a-input v-model:value="formState.zjm" class="w-[300px]" placeholder="标题" />
        </a-form-item>
        <a-form-item name="dateRange" label="下达日期">
          <a-range-picker v-model:value="formState.dateRange" class="w-[300px]" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex">
        <a-button class="mb-2" type="primary" @click="onEditOpen"> 新增 </a-button>
        <BaseExport type="防疫防护" name="信息下达" code="000677" class="ml-2" :search-params="formState">导出</BaseExport>
      </div>
      <BaseTable fill :data-source="tableData" :loading="loading" column-code="000677" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'documentName'">
            <div class="underline text-[#3758f1] cursor-pointer" :title="record.documentName" @click="downloadFile(record.documentUrl, record.documentName)">
              {{ record.documentName }}
            </div>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <a-button type="link" size="small" @click="onEditOpen(record)">修改</a-button>
              <BaseButton confirm-text="是否删除?" type="text" danger size="small" @click="onDelete(record)"> 删除 </BaseButton>
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="getData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'
import BaseButton from '@/components/BaseButton'
import Edit from './Edit'

const editOpen = ref(false)
const rowData = ref({})
const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})

const { loading, tableData, pagination, getData } = useTable({
  url: '/war/epidemicPrevention/getEpidemicMessageReleaseResponseList',
  immediate: true,
  formatParams: () => {
    return {
      ...formState.value,
      startDate: formState.value.dateRange?.[0],
      endDate: formState.value.dateRange?.[1]
    }
  }
})

const onEditOpen = (record = {}) => {
  rowData.value = { ...record }
  editOpen.value = true
}

// 删除
const onDelete = async (record) => {
  const res = await post('/war/epidemicPrevention/doDelEpidemicMessageRelease', { id: record.id })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}

const downloadFile = (url, title) => {
  fetch(url)
    .then((response) => response.blob())
    .then((blob) => {
      const url = window.URL.createObjectURL(blob)
      const a = document.createElement('a')
      a.href = url
      a.download = title // 设置下载的文件名
      document.body.appendChild(a)
      a.click()
      a.remove()
      window.URL.revokeObjectURL(url)
    })
    .catch()
}
</script>
